<template>
  <div class="changepassword_container">
    <div class="banner"></div>
    <div class="navbar">
      <div class="left" @click="$router.go(-1)"><van-icon name="arrow-left" /></div>
      <div class="center">{{ $route.query.title }}</div>
      <div class="left"><van-icon style="opacity: 0;" name="arrow-left" /></div>
    </div>

    <div class="card">
      <div class="item">
        <div class="label">项目</div>
        <input type="text" v-model="project" class="input" placeholder="请输入帐单名">
      </div>
      <div class="item">
        <div class="label">收支类型</div>
        <select name="fruits" class="input" @change="change">
          <option value="1">收入</option>
          <option value="2">支出</option>
        </select>
      </div>
      <div class="item">
        <div class="label">金额</div>
        <input type="text" v-model="price" class="input" placeholder="请输入金额   +100或-100">
      </div>
      <div class="item">
        <div class="label">日期</div>
        <input readonly @click="show = true" type="text" v-model="time" class="input" placeholder="选择日期">
      </div>
      <div class="button" @click="submit">
        <van-icon name="add" /><span>确认{{ $route.query.title.slice(0, 2) }}</span>
      </div>
      <div class="delbutton" @click="del" v-if="$route.query.title === '编辑账单'">
        <van-icon name="add" /><span>删除项目</span>
      </div>
    </div>

    <van-popup round v-model="show" get-container="body" position="bottom" :style="{ height: '50%' }">
      <van-datetime-picker @cancel="cancel" v-model="currentDate" @confirm="confirm" type="date" title="选择日期" :formatter="formatter" />
    </van-popup>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { Toast } from 'vant'
import { addBill,updateBill,delBill } from '@/api'
export default {
  name: 'ChangePassword',
  components: {},
  props: {},
  data() {
    return {
      project: '',
      price: '',
      time: '',
      type: '1',
      show: false,
      id: '',
      currentDate:''
    }
  },
  computed: {},
  watch: {
    type: {
      handler(value) {
        this.price = ''
        if (value === '1') {
          this.price = `+ ${this.price}`
        } else if (value === '2') {
          this.price = `- ${this.price}`
        }
      },
      immediate: true
    }
  },
  created() {

  },
  mounted() {
    if (this.$route.query.data) {
      this.project = this.$route.query.data.bill_name
      this.price = this.$route.query.data.price
      this.time = this.$route.query.data.time
      this.id = this.$route.query.data.id
    }
  },
  methods: {
    del() {
      delBill(this.id).then(res =>{
        if (res.code == 200) {
          Toast.success(res.msg)
          this.$router.push({ path: '/ascending' })
        } else {
          Toast.fail(res.msg)
        }
      })
     },
    formatter(type, val) {
      if (type === 'month') {
        return `${val}月`
      } else if (type === 'day') {
        return `${val}日`
      }
      return val
    },
    confirm() {
      this.time = dayjs(this.currentDate).format('YYYY-MM-DD')
      this.show = false
    },
    cancel() {
      this.show = false
    },
    change(val) {
      this.type = val.target.value
    },
    async submit() {
      if (this.id == '') {
        try {
          const res = await addBill({
            user_id: this.$store.getters.userInfo.id,
            user_name: this.$store.getters.userInfo.username,
            bill_name: this.project,
            price: this.price,
            time: this.time
          })
          if (res.code == 200) {
            this.$router.push({ path: '/ascending' })
            Toast.success(res.msg)
          } else {
            Toast.fail(res.msg)
          }
        } catch (error) {
          console.log(error);
        }
      } else {
        try {
          const res = await updateBill({
            user_id: this.$store.getters.userInfo.id,
            user_name: this.$store.getters.userInfo.username,
            bill_name: this.project,
            price: this.price,
            time: this.time,
            id:this.id
          })
          if (res.code == 200) {
            this.$router.push({ path: '/ascending' })
            Toast.success(res.msg)
          } else {
            Toast.fail(res.msg)
          }
        } catch (error) {
          console.log(error);
        }
      }
    }
  }
}
</script>
<style scoped lang='less'>
.changepassword_container {
  height: 100vh;
  overflow: hidden;
  position: relative;
  background-color: #f5f5f5;

  .banner {
    position: relative;
    width: 100%;
    overflow: hidden;
    height: 300px;
  }

  .banner::after {
    height: 300px;
    content: '';
    width: 200%;
    position: absolute;
    left: -50%;
    top: 0;
    border-radius: 0 0 50% 50%;
    background-image: url('../../../assets/image/WechatIMG196.jpg');
    background-size: cover;
  }

  .navbar {
    position: absolute;
    width: calc(100% - 60px);
    top: 20px;
    left: 0;
    padding: 30px;
    display: flex;
    justify-content: space-between;
    color: #fff;
    font-weight: 700;

    .center {
      font-size: 16px;
    }

    .left {
      font-size: 20px;
    }
  }

  .card {
    position: absolute;
    top: 130px;
    left: 50%;
    transform: translateX(-50%);
    padding: 20px 10px;
    width: 80%;
    margin: 0 auto;
    background-color: #fff;
    border-radius: 30px;
    box-shadow: 0 10px 10px rgba(0, 0, 0, 0.1);

    /* 添加阴影效果 */

    .item {
      margin-left: 20px;
      margin-top: 10px;
      margin-bottom: 20px;
      color: #7A7A7A;
      padding: 0 30px 0 0;

      .label {
        font-size: 14px;
      }

      .input {
        margin-top: 10px;
        width: 100%;
        height: 50%;
        border: 2px solid #E7E7E7;
        border-radius: 6px;
        font-size: 14px;
        padding: 10px 10px;
      }
    }

    .delbutton {
      text-align: center;
      font-size: 16px;
      color: red;
      padding: 10px 0;
      margin: 20px auto 0 auto;
      width: 60%;
      border: 2px solid red;
      border-radius: 6px;

      span {
        display: inline-block;
        width: 90px;
      }
    }

    .button {
      text-align: center;
      font-size: 16px;
      color: #438883;
      padding: 10px 0;
      margin: 20px auto 0 auto;
      width: 60%;
      border: 2px solid #438883;
      border-radius: 6px;

      span {
        display: inline-block;
        width: 90px;
      }
    }

  }
}
</style>
